﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace FunDraw.LayerInfos
{
    /// <summary>
    /// 图层尺寸类
    /// </summary>
    public class LayerSize
    {
        private int _Scale;
        private int _Margin;
        private int _Height;
        private int _Width;
        private int _xMax;
        private int _yMax;

        /// <summary>
        /// 坐标系刻度尺寸，单位是像素点数
        /// </summary>
        public int Scale
        {
            get
            {
                return _Scale;
            }
            set
            {
                _Scale = value;
            }
        }
        /// <summary>
        /// 坐标系外补丁，上下左右边框的距离
        /// </summary>
        public int Margin
        {
            get
            {
                return _Margin;
            }
            set
            {
                _Margin = value;
            }
        }
        /// <summary>
        /// 图层高度
        /// </summary>
        public int Height
        {
            get
            {
                return _Height;
            }
            set
            {
                _Height = value;
                //去掉上下补丁宽度除以二倍的刻度尺寸（因为存在上下正负两部分尺寸）
                _yMax = (int)Math.Floor((double)(_Height - 2 * _Margin) / (2 * _Scale));
            }
        }
        /// <summary>
        /// 图层宽度
        /// </summary>
        public int Width
        {
            get
            {
                return _Width;
            }
            set
            {
                _Width = value;
                //去掉左右补丁的长度除以二倍的刻度尺寸（因为存在正负两部分尺寸）
                _xMax = (int)Math.Floor((double)(_Width - 2 * _Margin) / (2 * _Scale));
            }
        }

        /// <summary>
        /// 最大的横坐标刻度
        /// </summary>
        public int xMax
        {
            get
            {
                return _xMax;
            }
            set
            {
                _xMax = value;
            }
        }

        /// <summary>
        /// 做大的纵坐标刻度
        /// </summary>
        public int yMax
        {
            get
            {
                return _yMax;
            }
            set
            {
                _yMax = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public LayerSize()
        {
            _Margin = 10;
            _Scale = 20;
            _Width = 800;
            _Height = 800;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="scale">刻度尺寸</param>
        /// <param name="margin">边距</param>
        /// <param name="height">高度</param>
        /// <param name="width">宽度</param>
        public LayerSize(int scale,int margin,int height,int width)
        {
            _Scale = scale>0?scale:20;
            _Margin = margin>0?margin:10;
            Height = height>10?height:10;
            Width = width>10?width:10;
        }

        /// <summary>
        /// 像素点转换成坐标点
        /// </summary>
        /// <param name="PixelPoint">限速点，所在平面的左上角为0，0不受外补丁的影响</param>
        /// <returns>坐标点</returns>
        public PointF PixelToCoordinate(PointF PixelPoint)
        {
            PointF center = new PointF(Width / 2, Height / 2);
            float x_new = (float)Math.Round((double)(PixelPoint.X - center.X) / _Scale, 2);
            float y_new = (float)Math.Round((double)(center.Y - PixelPoint.Y) / _Scale, 2);
            return new PointF(x_new, y_new);
        }

        /// <summary>
        /// 将坐标系点转换成像素点
        /// </summary>
        /// <param name="CoordinatePoint">坐标系点</param>
        /// <returns>像素点</returns>
        public PointF CoordinateToPixel(PointF CoordinatePoint)
        {
            PointF center = new PointF(Width / 2, Height / 2);
            float x_new = 0, y_new = 0;

            if (CoordinatePoint.X > 0)
                x_new = center.X + Math.Abs(CoordinatePoint.X) * _Scale;
            else
                x_new = center.X - Math.Abs(CoordinatePoint.X) * _Scale;

            if (CoordinatePoint.Y > 0)
                y_new = center.Y - Math.Abs( CoordinatePoint.Y) * _Scale;
            else
                y_new = center.Y + Math.Abs( CoordinatePoint.Y) * _Scale;

            return new PointF(x_new, y_new);
        }

    }
}
